<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="java.util.*,com.cap.bts.crmdp.dealer.meal.dto.MealLineDto,com.cap.bts.crmdp.dealer.coupon.domain.Ticket"
	pageEncoding="UTF-8"%>

<%
    MealLineDto mealLineDto = (MealLineDto) request
					.getAttribute("mealLineDto");

			ArrayList ticketList = (ArrayList) request
					.getAttribute("ticketList");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>套餐详细修改</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${layerPath}/jquery/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>
</head>

<script type="text/javascript">
	$(function() {
		$("#mealLineForm").validation({
			icon : true
		});
		//套餐详细信息修改
		$("#saveBtn")
				.click(
						function() {
							if ($("#mealLineForm").valid(this) == false) {
								return false;
							}
							var shortName = $("#shortName option:selected").text();
							$
									.ajax({
										url : '${webcontext}/crmdp/dealer/meal/updateMealLineInfo',
										type : 'post',
										data : {
											'id' : $("#id").val(),
											'packageDefId':$("#packageDefId").val(),
											'ticketDefId':$("#shortName").val(),
											'shortName':shortName,
											'numberOfUse':$("#numberOfUse").val(),
											'prepayAmount':$("#prepayAmount").val()
										},
										success : function(data) {
											if (data.code == true) {
												jAlert(
														data.msg,
														"提示",
														function() {
															var packageDefId = $("#packageDefId").val();
															var qName = $("#qName").val();
															var qCode = $("#qCode").val();
															var qStatus = $("#qStatus").val();
															var pager = $("#pager").val();
															parent.location.href = '${webcontext}/crmdp/dealer/meal/toEditMealPage?id='
																	+ packageDefId+"&qName="+qName+"&qCode="+qCode+"&qStatus="+qStatus+"&pager.currentPage="+pager;
														});
											} else if (data.code == false) {
												jAlert(data.msg, "提示");
											}
										},
										error : function() {
											jAlert("服务器错误，请联系管理员", "提示");
										},
									});
						});
		
		$("#exitBtn").click(function(){
			var packageDefId = $("#packageDefId").val();
			var qName = $("#qName").val();
			var qCode = $("#qCode").val();
			var qStatus = $("#qStatus").val();
			var pager = $("#pager").val();
			parent.location.href = '${webcontext}/crmdp/dealer/meal/toEditMealPage?id='
					+ packageDefId+"&qName="+qName+"&qCode="+qCode+"&qStatus="+qStatus+"&pager.currentPage="+pager;
		});
	});

	function exitBtn() {
		window.parent.search();
	}

	window.onload = function() {
		var ticketDefId = '${mealLineDto.ticketDefId}';
		if (ticketDefId != null && ticketDefId != "") {
			$("#shortName").val(ticketDefId);
		}
	};
	
	//选中项目列表中的值，改变类型的值
	function changeTicketType(value) {
		<%if (ticketList != null) {%>
			<%for (int k = 0; k < ticketList.size(); k++) {
					Ticket ticket = (Ticket) ticketList.get(k);%>
				if (value == <%=ticket.getId()%>) {
					var type = '<%=ticket.getTicketType()%>';
			$("#ticketType").val(type);
			changePrepayAmount("prepayAmount",
<%=ticket.getId()%>
	);
		}
<%}%>
	
<%}%>
	}

	//改变预付金额的值
	function changePrepayAmount(id, ticketId) {
		$.ajax({
			url : '${webcontext}/crmdp/dealer/meal/getTicKetLineInfo',
			type : 'post',
			data : {
				'ticketId' : ticketId
			},
			success : function(data) {
				if (data.code == true) {
					var prepayAmount = data.prepayAmount;
					$("#onePrice").val(prepayAmount);
					$("#prepayAmount").val(prepayAmount);
				} else if (data.code == false) {
					jAlert(data.msg, '提示', function() {
						$("#numberOfUse").val(0);
						$("#onePrice").val(0);
						$("#prepayAmount").val(0);
					});
				}
				;
			},
			error : function(data) {
				jAlert(data.msg, '提示');
			}
		});
	}

	//修改数量，改变预付金额的值
	function editPrepayAmount() {
		var numberOfUse = $("#numberOfUse").val();
		if (numberOfUse != null && numberOfUse != "") {
			if (!isNaN(numberOfUse)) {
				var onePrice = $("#onePrice").val();
				sumAmount = parseFloat(numberOfUse) * parseFloat(onePrice);
				$("#prepayAmount").val(fmoney(sumAmount, 1));
				$("#onePrice").val(fmoney(onePrice, 1));
			}
		}
	}

	//格式化金额，保留两位小数点
	function fmoney(s, n) {
		n = n > 0 && n <= 20 ? n : 2;
		s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
		var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
		t = "";
		for ( var i = 0; i < l.length; i++) {
			//t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
			t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "" : "");
		}
		return t.split("").reverse().join("") + "." + r;
	}
</script>
<style>
.labelShow {
	float: left;
	width: 20%;
	line-height: 30px;
	text-align: right;
}

.inputDiv {
	float: left;
	width: 50%;
}

.main {
	position: relative;
	margin: 0 auto;
	padding: 10px 10px;
	width: 570px;
	height: 350px;
}
</style>
<body>
	<div class="main">
		<form:form class="form-horizontal" id="mealLineForm" method="post"
			action="" modelAttribute="dto" role="form">
			<input type="text" id="id" value="${mealLineDto.id}" class="hidden" />
			<input type="text" id="packageDefId"
				value="${mealLineDto.packageDefId}" class="hidden" />
			<input type="text" class="hidden" name="qName" id="qName" value="${qName}">
			<input type="text" class="hidden" name="qCode" id="qCode" value="${qCode}">
			<input type="text" class="hidden" name="qStatus" id="qStatus"
				value="${qStatus}">
			<input type="text" class="hidden" id="pager" name="pager.currentPage"
				value="${currentPage}">
			<div class="form-group">
				<label for="shortName" class="col-sm-2 control-label labelShow">项目</label>
				<div class="col-sm-5 inputDiv">
					<select id="shortName" name="shortName"
						class="form-control input-sm"
						onchange="changeTicketType(this[selectedIndex].value)">
						<core:forEach items="${ticketList}" var="list">
							<option value="${list.id}">${list.ticketName}</option>
						</core:forEach>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label for="ticketType" class="col-sm-2 control-label labelShow">类型</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control" id="ticketType"
						value="${mealLineDto.ticketType}" readonly />
				</div>
			</div>
			<div class="form-group">
				<label for="numberOfUse" class="col-sm-2 control-label labelShow">数量</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control text-right" id="numberOfUse"
						value="${mealLineDto.numberOfUse}" check-type="number"
						required-message="请填写数量" onchange="editPrepayAmount()">
				</div>
			</div>
			<div class="form-group">
				<label for="onePrice" class="col-sm-2 control-label labelShow">单价</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control text-right" id="onePrice"
						value="${mealLineDto.onePrice}" readonly check-type="required"
						required-message="单价不能为空">
				</div>
			</div>
			<div class="form-group">
				<label for="prepayAmount" class="col-sm-2 control-label labelShow ">预付金额</label>
				<div class="col-sm-5 inputDiv">
					<input type="text" class="form-control text-right"
						id="prepayAmount" value="${mealLineDto.prepayAmount}" readonly
						check-type="required" required-message="预付金额不能为空">
				</div>
			</div>
			<div class="form-group text-center" style="margin-left: -100px;">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="button" class="btn btn-success" id="saveBtn">保存</button>
					<button type="button" class="btn btn-success" id="exitBtn">取消</button>
				</div>
			</div>
		</form:form>
	</div>
</body>
</html>